﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading;

namespace IRCBot
{
    public partial class ConsoleLog : Form
    {

        void ConnectGlider(string server, int port)
        {
            Log("Opening connection to Glider");
            gClient = new TcpClient(server, port);
            if (gClient.Connected)
            {
                Log("Connected to Glider");
                gT = new Thread(new ThreadStart(GliderHandler));
                gT.Start();
            }
        }

        void GliderHandler()
        {
            gStream = gClient.GetStream();
            gOutput = new StreamWriter(gStream);
            gInput = new StreamReader(gStream);

            // Time to identify ourself with password.
            gOutput.WriteLine(gPass);
            gOutput.Flush();

            while (gClient.Connected)
            {
                string input = gInput.ReadLine();

                string[] result = input.Split(' ');

                if (input != "---" && input.Contains(" "))
                {
                    string var = result[0];
                    string val = result[1];
                    if (input == "Authenticated ok")
                    {
                        gCon = true;
                        Message(ircChannel, "Connected to Glider");
                        gCommand("/status");
                    }

                    switch (var)
                    {
                        case "Health:":
                            Health = (Double.Parse(val) * 100);
                            break;

                        case "Mana:":
                            Mana = Int32.Parse(val);
                            break;

                        case "Name:":
                            Character = val;
                            break;

                        case "Class:":
                            Class = val;
                            break;

                        case "Level:":
                            Level = val;
                            break;

                        case "Attached:":
                            Attached = Boolean.Parse(val);
                            break;

                        case "Experience:":
                            XP = Int32.Parse(val);
                            break;

                        case "Next-Experience:":
                            NextXP = Int32.Parse(val);
                            break;
                    }
                }
            }
        }
    }
}